package com.coolxi.upload;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by Eoly on 2017/6/13.
 */
public abstract class WebImgUpload extends WebFileUpload {

    public WebImgUpload(MultipartFile img, HttpServletRequest request, String path) {
        super(img, request, path);
    }

    public WebImgUpload(MultipartFile img, HttpServletRequest request, String path, boolean isRandomName) {
        super(img, request, path, isRandomName);
    }

    private boolean checkExtensionName() {
        String extensionName = getExtensionName();
        return extensionName.equals("png") || extensionName.equals("png") || extensionName.equals("jpg") || extensionName.equals("jpeg") || extensionName.equals("gif");
    }

    protected abstract void doCheckFail();

    @Override
    public void upload() throws IOException {
        if (checkExtensionName()) {
            super.upload();
        } else {
            doCheckFail();
        }
    }
}
